#include <stdio.h>
#include <stdlib.h>
#include "single_list.h"

struct test
{
    char name[64];
    int  num;
};

_SListRet destroy(void *data1)
{
    struct test *a = (struct test*)data1;
    free(a);
    return _SLIST_RET_OK_;
}
_SListRet print(void *data1, void *data2)
{
    struct test *a = (struct test*)data2;
    printf("%d:\t%s\n", a->num, a->name);
    return _SLIST_RET_OK_;
}

_SListRet to_lower(void *data1, void *data2)
{
    struct test *a = (struct test*)data2;
    char *b = a->name;

    while(*b != '\0')
    {
	if(isupper(*b))
	{
	    *b = tolower(*b);
	}

	b++;
    }

    return _SLIST_RET_OK_;
}

_SListRet get_max(void *data1, void *data2)
{
    int *a = (int *)data1;
    struct test *b = (struct test *)data2;

    if (*a < b->num)
    {
	*a = b->num;
    }
    
    return _SLIST_RET_OK_;
}

_SListRet cmp(void *data1, void *data2)
{
    struct test *a = (struct test *)data1;
    struct test *b = (struct test *)data2;
    if (a->num == b->num)
    {
	return _SLIST_EQUEAL_;
    }
    else if (a->num > b->num)
    {
	return _SLIST_LARGER_THAN_;
    }
    else 
    {
	return _SLIST_LESS_THAN_;
    }
}


int main(int argc, char *argv[])
{
    struct test *data = NULL;
    _SNode *node = NULL;
    _SList list = NULL;
    int i = 0;

    list = create_list(cmp);

    for(i; i < 10; i++)
    {
	data = (struct test *)malloc(sizeof(struct test));
	printf("please input a number:\n");
	scanf("%d", &(data->num));
	printf("please input the description:\n");
	scanf("%s", data->name);
	slist_append(list, data);
    }

    slist_foreach(list, print, NULL);
    i = 0;
    slist_foreach(list, get_max, &i);
    printf("Max: %d\n", i);
    i = 0;
    printf("Please input the number you want to delete:\n");
    struct test del;
    scanf("%d", &(del.num));
    delete_node(list, destroy, (void *)(&del));
    slist_foreach(list, print, NULL);
    slist_foreach(list, get_max, &i);
    printf("Max: %d\n", i);
    destroy_list(list, destroy);

    return 0;
}

